<?php 
session_start();

 include '../Model/Dal/DataAccess.php';

  $fun = new DataAccess();
  
  if (isset($_GET['login'])) {
    //get the posted values
    $user_name = htmlspecialchars($_POST['user_name'], ENT_QUOTES);
   
    //$pass=md5($_POST['password']);
    $pass = $_POST['password'];

    $result = $fun->confirmarUsuario($user_name, $pass);
    
    
    if ($result != "fallo") {
        _setSession($result);
        echo "|-|ok|-|" . $result['tipo'];
    } else {
        //$result['tipo'];
        echo "fallo";
    }
} else {
    if (isset($_GET['logout'])) {
//        if (!isset($_SESSION['uid'])) {
//            $this->session_defaults();
//        }
        session_defaults();
        session_destroy();
	header("Location:../View/index.php");
    }
}



function session_defaults() {
    $_SESSION['logged'] = false;
    $_SESSION['uid'] = 0;
    $_SESSION['username'] = '';
    $_SESSION['tipoUsuario'] = '';
}


function _setSession($values) {
    //$this->id = $result['id'];
    $_SESSION['uid'] = $values['cedula'];
    $_SESSION['username'] = $values['username'];
    $_SESSION['tipoUsuario'] = $values['tipo'];
    $_SESSION['logged'] = true;
}

?>